#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
/*----------------------------------------------------------------------------*/

char * add( char * lhs, char * rhs );

/*----------------------------------------------------------------------------*/

int main(void)
{

add( "99987", "56903" );

getch();
return 0;
}

char * add( char * lhs, char * rhs )
{

unsigned int len1 = 0, len2 = 0;

while( lhs[len1] ) len1++;
while( rhs[len2] ) len2++;

unsigned int size = ( len1 > len2 ? len1 : len2 ) + 2;
char * ans = new char[ size ];

int j = len1-1,
    k = len2-1,
    num1  = 0,
    num2  = 0,
    sum   = 0, 
    carry = 0;

cout << setw(10) << lhs << endl;
cout << setw(10) << rhs << endl << endl;

for( unsigned int i = 0 ; i < len1 && i < len2 ; i++ )
{
    num1  = lhs[j--] - '0';
    num2  = rhs[k--] - '0';
    sum   = carry + num1 + num2;

    if( sum >= 10 )
    {
        carry = 1;
        sum = sum - 10;
    } 
    else
    {
        carry = 0;
    }
    
    ans[i] = sum + '0';
}

ans[size-2] = carry ? ( carry + '0' ) : '\0';
ans[size-1] = '\0';

size -= ( ans[size-2] == '\0' )? 2 : 1;

for( unsigned int start = 0, end = size-1 ; start < size/2 ; start++, end-- )
{
    const char temp       = ans[start];
               ans[start] = ans[end];
               ans[end]   = temp;
}

cout << setw(10) << ans ;

delete [] ans;
}
/*----------------------------------------------------------------------------*/
